package com.cskaoyan.wx.controller;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.wx.service.WxCouponService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.WxCouponReceiveVo;
import com.cskaoyan.vo.WxCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 作者：whl
 * 功能：优惠券
 */
@RestController
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    /**
     * by:whl
     * 功能：优惠券列表【get】
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("wx/coupon/list")//list?page=1&size=10
    public BaseRespVo list(Integer page,Integer size){
        WxCouponVo wxCouponVo = wxCouponService.list(page,size);
        return BaseRespVo.ok(wxCouponVo);
    }

    /**
     * by:whl
     * 功能：我的优惠券列表【get】
     * @param status
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("wx/coupon/mylist")//wx/coupon/mylist?status=0&page=1&size=10
    public BaseRespVo mylist(Short status,Integer page,Integer size){
        WxCouponVo wxCouponVo = wxCouponService.mylist(status,page,size);
        return BaseRespVo.ok(wxCouponVo);
    }

    /**
     * by:whl【未完成】
     * 功能：当前订单可用优惠券列表【Get】
     * @param cartId
     * @param grouponRulesId
     * @return
     */
    @RequestMapping("wx/coupon/selectlist")//cartId=85&grouponRulesId=0
    public BaseRespVo selectlist(Integer cartId,Integer grouponRulesId){
        //假装有userId
        int userId = 1;
        List<Coupon> couponList = wxCouponService.selectList(userId,grouponRulesId);
        return BaseRespVo.ok(couponList);
    }

    /**
     * by:whl
     * 功能：优惠券领取【Post】
     * 请求参数:couponId
     * @return
     */
    @RequestMapping("wx/coupon/receive")
    public BaseRespVo receive(@RequestBody CouponUser couponUser){
        //假设有userId
        int userId = 1;
        Integer couponId = couponUser.getCouponId();
        int code = wxCouponService.receive(userId,couponId);
        //查看是否领取过该优惠券
        if (code == 0){
            return BaseRespVo.ok();
        }
        if (code == 740){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(740);
            baseRespVo.setErrmsg("优惠券已经领取过");
            return baseRespVo;
        }

        //若已领取看是否有上限
        return BaseRespVo.ok();

    }

    /**
     * by:whl
     * 功能：优惠券兑换【Post】
     * 请求参数：code
     * @return
     */
    @RequestMapping("wx/coupon/exchange")
    public BaseRespVo exchange(@RequestBody Coupon coupon){
        int code = wxCouponService.exchange(coupon.getCode());
        if (code == 0){
            return BaseRespVo.ok();
        }
        if (code == 740){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(740);
            baseRespVo.setErrmsg("优惠券已兑换");
            return baseRespVo;
        }
        if (code == 742){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(742);
            baseRespVo.setErrmsg("优惠券不正确");
            return baseRespVo;
        }

        return BaseRespVo.ok();
    }
}
